﻿using Logistics.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Logistics.Model;
using Logistics.Commn;
using Microsoft.Extensions.Logging;
using ServiceStack;
using Logistics.Commn.RabbitMQ;
using HS.RealEstate.RabbitMQ;

namespace Logistics.SignIn
{
    public class SignInRepository : ISignInRepository
    {
        private readonly LogisticsDbContext _db;

        public SignInRepository(LogisticsDbContext db)
        {
            _db = db;
        }

        /// <summary>
        /// 登录接口实现类
        /// </summary>
        /// <param name="accountNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<Operator> SignInAsync(string accountNumber, string password)
        {
            //获取登录人数据
            password = MD5Helper.Get16MD5One(password);
            var oper = await _db.Operator.FirstOrDefaultAsync(a => a.Account == accountNumber && a.PassWord == password);
            //判断是否存在
            if (oper == null)
            {
                return null;
            }

            //将登录员工信息存放在RedisHelper
            RedisHelper.Set<Operator>("Operator", oper);
            return  oper;
        }

        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <param name="log"></param>
        public void LoginLog(UserLog log)
        {
             _db.UserLog.Add(log);
             _db.SaveChanges();
        }


        /// <summary>
        /// 验证账号是否存在
        /// </summary>
        /// <param name="accountNumber"></param>
        /// <returns></returns>
        public async Task<int> VerifyAccountNumber(string accountNumber)
        {
            var i =await  _db.Operator.CountAsync(a => a.Account==accountNumber);
            if (i > 0)
            {
                RedisHelper.Set("accountNumber", accountNumber);
            }

            return i;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailbox"></param>
        public  void SendMailbox(string mailbox)
        {
            var randomNumber = Rand.Str(6);
            var userName = RedisHelper.Get("accountNumber");
            RedisHelper.Set("randomNumber", randomNumber,DateTime.Now.AddSeconds(60));
            Task.Run(() =>
            {
                SendRouting.SendMessageDirect(userName, mailbox, randomNumber);
                RecevieRouting.DirectConsumer();
            });
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="accountNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async  Task<int> PutChangePassword(string accountNumber, string password)
        {
            var opr = _db.Operator.FirstOrDefault(a => a.Account == accountNumber);
            if (opr != null)
            {
                opr.PassWord = MD5Helper.Get16MD5One(password);
            }
            int i =await _db.SaveChangesAsync();
            return i;
        }
    }
}